package chan.project.smartlawdatamanage.controller;

import chan.project.smartlawcommon.annotation.AuthCheck;
import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.exception.BusinessException;
import chan.project.smartlawdatamanage.service.LawRegulationService;
import chan.project.smartlawdatamanage.knowledge.LawRegulationIndex;
import chan.project.smartlawmodel.dto.lawregulation.LawRegulationAddRequest;
import chan.project.smartlawmodel.entity.LawRegulation;
import chan.project.smartlawmodel.vo.LawRegulationVO;
import chan.project.smartlawmodel.vo.UserInfoVO;
import chan.project.smartlawservice.userService.UserFeignClient;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Tag(name = "数据请求接口")
@RestController
@RequestMapping("data")
public class LawController {


    @Resource
    private LawRegulationService lawRegulationService;

    @Resource
    private UserFeignClient userFeignClient;

    /**
     * 管理员传入法律法规存储到数据库中，并更新搜索引擎
     * @param lawRegulationAddRequest
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("addLaw")
    public Boolean addLaw(@RequestBody LawRegulationAddRequest lawRegulationAddRequest, HttpServletRequest request){
        LawRegulation lawRegulation = new LawRegulation();
        BeanUtils.copyProperties(lawRegulationAddRequest, lawRegulation);
        //查找最新的版本号，如果存在就存储时版本号+1
        LawRegulation latest = lawRegulationService.lambdaQuery()
                .orderByDesc(LawRegulation::getVersion)
                .last("LIMIT 1")
                .one();
        if(latest != null){
            lawRegulation.setVersion(latest.getVersion() + 1);
        }else {
            lawRegulation.setVersion("1");
        }
        UserInfoVO loginUser = userFeignClient.getLoginUser();
        lawRegulation.setCreatedBy(loginUser.getId().toString());
        // 存储新的法律法规到数据库中
        lawRegulationService.save(lawRegulation);
        LawRegulationIndex lawRegulationIndex = new LawRegulationIndex();
        BeanUtils.copyProperties(lawRegulation, lawRegulationIndex);
        return true;
    }

    /**
     * 获取所有的法律法规名称
     * @return
     */
    @AuthCheck(mustRole = "data_admin")
    @GetMapping("/getLaw")
    public List<String> getAllLawFileName(){
        List<String> list = new ArrayList<>();
        List<LawRegulation> lawRegulations = lawRegulationService.list();
        if(CollectionUtil.isEmpty(lawRegulations)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"数据库还未存储法律法规");
        }
        for (LawRegulation lawRegulation : lawRegulations) {
            list.add(lawRegulation.getTitle());
        }
        return list;
    }

    /**
     * 删除某一条法律法规
     * @param id
     * @return
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/deleteLaw")
    public boolean deleteLawFile(@RequestParam("id") Long id){
        LawRegulation byId = lawRegulationService.getById(id);
        if(ObjectUtil.isEmpty(byId)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"法律法规已经删除");
        }
        boolean b = lawRegulationService.removeById(id);
        return b;
    }

    /**
     * 获取某一条法律法规的版本链
     * @return
     */
    @AuthCheck(mustRole = "data_admin")
    @GetMapping("/lawChain")
    public List<LawRegulationVO> getLawVersionChain(@RequestParam("lawName") String lawName){
        LambdaQueryWrapper<LawRegulation> lawRegulationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        lawRegulationLambdaQueryWrapper.eq(LawRegulation::getTitle,lawName);
        lawRegulationLambdaQueryWrapper.orderBy(true,true,LawRegulation::getVersion);
        List<LawRegulation> list = lawRegulationService.list(lawRegulationLambdaQueryWrapper);
        return LawRegulationVO.objListToVoList(list);
    }

    @GetMapping("getUserRoleTest")
    private String getUserRoleTest(HttpServletRequest request){
        UserInfoVO loginUser = userFeignClient.getLoginUser();
        return loginUser.getUserRole();
    }
}
